﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Mono.Cecil;

namespace LinFuBox 
{
    class AroundInvoke : IAroundInvoke
    {
        public void BeforeInvoke(IInvocationInfo info)
        {
            Console.WriteLine("Before Invoke");
        }

        public void AfterInvoke(IInvocationInfo info, object returnValue)
        {
            Console.WriteLine("After Invoke");
        }
    }

    class Program 
    {
        static void Main(string[] args) 
        {
            const string sourceFilePath = @"E:\Code\LiveSource\build\Debug\LiveSource.UnitTests.dll";
            const string destinationFilePath = @"E:\Code\LiveSource\build\Debug\LiveSource.UnitTests_2.dll";
            File.Copy(destinationFilePath, sourceFilePath, true);
            AssemblyDefinition assembly = AssemblyFactory.GetAssembly(sourceFilePath);

//            Func<TypeReference, bool> typeFilter = delegate(TypeReference typeReference) { return true; };
            Func<TypeReference, bool> typeFilter = (typeReference) => true;
            Func<MethodReference, bool> methodFilter = (methodReference) => true;
            Func<MethodReference, bool> methodCallFilter = (methodReference) => true;

            foreach (TypeDefinition type in assembly.MainModule.Types)
            {
                foreach (MethodDefinition method in type.Methods)
                {
                    method.InterceptMethodCalls(typeFilter, methodFilter, methodCallFilter);
                }
            }

            AssemblyFactory.SaveAssembly(assembly, sourceFilePath);
        }
    }
}
